package com.huluobo.high3videoproject.mvp.goods.presenter;

import com.huluobo.base.view.BasePresenter;
import com.huluobo.high3videoproject.bean.GoodsInfoBeanResult;
import com.huluobo.high3videoproject.mvp.goods.model.GoodsInfoModel;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class GoodsInfoPresenter extends BasePresenter<GoodsInfoContract.View> implements GoodsInfoContract.Presenter {
    private GoodsInfoModel goodsInfoModel = new GoodsInfoModel();

    @Override
    public void getGoodsInfo(int category_id, int currentPage, int pageSize) {
        goodsInfoModel.getGoodsInfo(category_id, currentPage, pageSize).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<GoodsInfoBeanResult>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        compositeDisposable.add(d);
                    }

                    @Override
                    public void onNext(GoodsInfoBeanResult goodsInfoBeanResult) {
                        if (isAttach()) {
                            getView().onGetGoodsInfoSuccess(goodsInfoBeanResult);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        if (isAttach()) {
                            getView().onGetGoodsInfoFailed(e.getMessage());
                        }
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}
